﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Server
{
    public sealed class Logger
    {
        static readonly Logger instance = new Logger();

        private ServerDataBaseContainer _connection;

        static Logger()
        {
            
        }

        Logger()
        {
            _connection = new ServerDataBaseContainer();
        }

        public void Log(string message, bool isError = false)
        {
            _connection.ServerLogSet.AddObject(new ServerLog
            {
                Message = message,
                IsError = isError,
                Date = DateTime.Now
            });
            _connection.SaveChanges();

        }

        public void Log(Exception ex)
        {
            this.Log(ex.ToString(), true);
        }

        public void ClientLog(string message, Guid clientId, int? compiutingId = null, bool isError = false)
        {
            _connection.ClientLogSet.AddObject(new ClientLog
            {
                Message = message,
                IsError = isError,
                Date = DateTime.Now,
                CompiutingUnits = _connection.CompiutingUnitsSet.First(u => u.Guid == clientId),
                CompiutingId = compiutingId
            });
            _connection.SaveChanges();
        }

        public static Logger Instance
        {
            get
            {
                return instance;
            }
        }
    }
}
